package com.zhuxulin.tab.busness;

import java.io.File;
import java.io.IOException;

import android.content.Context;
import android.media.MediaPlayer;

public final class MidiPlayer {
	private String audio = null;
	private boolean systemIsPlaying = false;
	private boolean isPianoPlaying = false;
	private Context context = null;
	private static MidiPlayer player = new MidiPlayer();
	private MediaPlayer mPlayer = null;

	private MidiPlayer() {
		mPlayer = new MediaPlayer();
	}

	public MidiPlayer getMidiPlayer() {
		return player;
	}

	public void setFile(String fileName) {
		this.audio = fileName;
	}

	public void setFile(File srcFile) {
		this.audio = srcFile.getPath();
	}

	public void pianoPause() {

	}

	public void pianoStop() {

	}

	public void pianoPlay() {
		if (isPianoPlaying) {

		} else {

		}
	}

	public void pianoStart() {

	}

	public boolean isPianoPlaying() {
		return isPianoPlaying;
	}

	public void systemPause() {
		if (systemIsPlaying) {
			mPlayer.pause();
		}
	}

	public void systemStop() {
		if (systemIsPlaying) {
			mPlayer.stop();
		}
	}

	public void systemPlay() {
		if (systemIsPlaying) {
			mPlayer.start();
		}
	}

	public void systemStart() throws IllegalArgumentException,
			SecurityException, IllegalStateException, IOException {
		if (systemIsPlaying) {
			systemPlay();
		} else {
			systemIsPlaying = true;
			resetSystemMediaPlayer();
			mPlayer.start();
		}
	}

	public boolean systemIsPlaying() {
		return systemIsPlaying;
	}

	private void resetSystemMediaPlayer() throws IllegalArgumentException,
			SecurityException, IllegalStateException, IOException {
		mPlayer.reset();
		mPlayer.setDataSource(audio);
		mPlayer.prepare();
	}

	public void setContext(Context context) {
		this.context = context;
	}
}
